<?php
namespace ac\modules\acount\controllers;

use Yii;
use ac\controllers\AuthController;
use app\models\Admin;
use ac\component\exception\ErrMap;
use ac\component\tool\Tool;
use app\models\OperationLog;

class AcountController extends AuthController
{
	/*账户信息*/
    public function actionInfo()
    {
    	$userInfo = $this->session->get('loginInfo')->userInfo;
    	$info = Admin::find()->select(['username', 'role_id', 'avatar', 'lastloginip', 'lastlogin_time', 'creater', 'logintimes', 'add_time'])
    		->where('admin_id = :id', [':id' => $userInfo['admin_id']])
    		->asArray()
    		->one();

        if(!is_null($info)){
        	return Tool::showRes([0, '获取成功'], $info);
        }
        return Tool::showRes(ErrMap::getError(10010));
    }

    /*修改会员信息*/
    public function actionModifyInfo()
    {
        if(Yii::$app->request->isPost){
            $id = $this->session->get('loginInfo')->userInfo['admin_id'];

            $admin = new Admin;

            if($admin->modifyAdmin($this->post, $id)){
                $backInfo = [0, '修改信息成功'];//返回信息
            }else{
                if($admin->hasErrors()){
                    $backInfo = ErrMap::getError(10005);//返回信息
                    $exception = $admin->getErrors();//异常信息
                    if(array_key_exists("oldpassword", $exception)){
                        $backInfo = ErrMap::getError(10024);//返回信息
                    };
                }
            }

            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
                return $res;
            };

        }

        return Tool::showRes(ErrMap::getError(10005));
    }
}
